package com.eb.buybuybuy.mapper;

import com.eb.buybuybuy.annotation.Component;
import com.eb.buybuybuy.bean.Goods;
import com.eb.buybuybuy.util.MysqlUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Component
public class GoodsMapper {
    public Goods getById(Integer id) throws ReflectiveOperationException, SQLException {
        List<Goods> list = MysqlUtil.query("select * from goods where id = ?", Goods.class, id);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<Goods> list() throws ReflectiveOperationException, SQLException {
        return MysqlUtil.query("select * from goods", Goods.class);
    }

    public void decreaseNumber(Integer goodsId, Integer number) throws SQLException {
        MysqlUtil.update("update goods set quantity = quantity - ? where id = ?", number, goodsId);
    }
}
